﻿
@{
    ViewData["Title"] = "Index";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            background-color: #f9f9f9;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }

        th {
            background-color: #f4f4f4;
            font-weight: bold;
        }

        .toolbar {
            margin-bottom: 10px;
        }

            .toolbar input[type="text"] {
                margin-right: 10px;
            }

            .toolbar button {
                margin-right: 5px;
                background-color: #007bff;
                color: white;
                border: none;
                padding: 8px 12px;
                cursor: pointer;
                border-radius: 4px;
            }

                .toolbar button:hover {
                    background-color: #0056b3;
                }

        .actions button {
            background-color: #dc3545;
            color: white;
            border: none;
            padding: 5px 10px;
            cursor: pointer;
            border-radius: 4px;
        }

            .actions button:hover {
                background-color: #c82333;
            }
    </style>
</head>
<body>

    <h2>其他入库</h2>
    <br />
    <div class="toolbar">
        <button id="searchButton">查询</button>
        <button id="addButton">录入</button>
        <button id="updateButton">编辑</button>
    </div>

    <table class="layui-hide" id="mainData" lay-filter="mainData" style="margin-top: 10px;margin-left: 20px">
    </table>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-orange layui-btn-xs" lay-event="delete">删除</a>
    </script>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.5.6/layui/layui.all.js"></script>

    <script>
        layui.use(['table', 'form'], function () {
            var table = layui.table;
            var form = layui.form;

            // 渲染数据表格
            function showData() {
                table.render({
                    elem: '#mainData',
                    url: '/PurchaseWarehousing/GetPurchaseWarehousing',
                    page: true,
                    limit: 10,
                    limits: [10, 20, 30],
                    cols: [[
                        { field: 'ownerOfGoods', title: '货主', width: 120, sort: true, align: 'center' },
                        { field: 'estimatedTime', title: '预计到货时间', width: 120, align: 'center' },
                        { field: 'orderNumber', title: '客户订单号', width: 120, align: 'center' },
                        { field: 'transportation', title: '运输公司', width: 120, align: 'center' },
                        { field: 'supplier', title: '供应商', width: 120, align: 'center' },
                        { field: 'reserveArea', title: '预约库区', width: 120, align: 'center' },
                        { field: 'warehouse', title: '仓库', width: 120, align: 'center' },
                        { field: 'orderType', title: '订单类型', width: 120, align: 'center' },
                        { field: 'productCode', title: '商品编码', width: 120, align: 'center' },
                        { field: 'age', title: '数量', width: 120, align: 'center' },
                        { field: 'sdate', title: '状态', width: 120, align: 'center' },
                        { field: 'orderName', title: '商品名称', width: 120, align: 'center' },
                        { field: 'batch', title: '批次', width: 120, align: 'center' },
                        { field: 'positionOfStorage', title: '储位', width: 120, align: 'center' },
                        { field: 'pallet', title: '托盘', width: 120, align: 'center' },
                        { field: 'unit', title: '单位', width: 120, align: 'center' },
                        { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150, align: 'center' }
                    ]],
                    parseData: function (res) {
                        console.log(res)
                        return {
                            code: res.code,
                            count: res.data.currentPage,
                            data: res.data.queryable
                        };
                    },
                });
            }

            showData();

            //监听工具条
            table.on('tool(mainData)', function (obj) {
                var data = obj.data;
                if (obj.event === 'delete') {
                    layer.confirm('确定删除该供应商吗？', function (index) {
                        // 使用 AJAX 请求实现删除
                        $.ajax({
                            url: '/Vendor/DeleteVendor',
                            type: 'POST',
                            data: { id: data.id },
                            success: function (res) {
                                if (res==1) {
                                    layer.msg('删除成功', { icon: 1, time: 1000 }, function () {
                                        showData(); // 刷新表格
                                    });
                                } else {
                                    layer.msg('删除失败: ' + res.msg, { icon: 2, time: 2000 });
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
            });

            // 查询
            $("#searchButton").on("click", function () {
                showData();
            });

            // 录入
            $("#addButton").on("click", function () {
                layer.open({
                    type: 2,
                    area: ['1350px', '80%'],
                    title: '入库管理',
                    shade: 0.6,
                    shadeClose: true,
                    maxmin: true,
                    anim: 0,
                    content: '/PurchaseWarehousing/Add',
                    end: function () {
                        location.reload();
                    }
                });
            });


        // 编辑
        $("#updateButton").on("click", function () {
            var checkStatus = table.checkStatus('mainData');
            var data = checkStatus.data;

            if (data.length === 0) {
                layer.msg('请选择要编辑的供应商', { icon: 0, time: 2000 });
                return;
            } else if (data.length > 1) {
                layer.msg('只能选择一个供应商进行编辑', { icon: 2, time: 2000 });
                return;
            }

            // 获取选中的供应商ID
            var id = data[0].id;

            // 打开编辑页面并传递该供应商的ID
            layer.open({
                type: 2,
                area: ['800px', '70%'],
                title: '编辑供应商',
                shade: 0.6,
                shadeClose: true,
                maxmin: true,
                anim: 0,
                content: '/Vendor/Update?id=' + id, // 传递ID
                end: function () {
                    showData(); // 刷新表格
                }
            });
        });

        });
    </script>

</body>
</html>

